var houseMap = new Array();

function setFlat() {
    ableSetFlat = true;
}

function addHouse(latlng, map) {
    ableSetFlat = false;
    var request = makeRequest();
    request.open("POST", "/storeHouses", false);
    var flat = new Flat();
    flat.lat = latlng.lat();
    flat.lng = latlng.lng();
    request.send(flat2Text(flat));
    screenMoveEnd(map);
}

function moveHouse(flat) {
    var request = makeRequest();
    request.open("POST", "/storeHouses", true);
    request.send(flat2Text(flat));
}

function createHouse(map, latlng, flat) {
    var houseIcon = new GIcon(G_DEFAULT_ICON);
    if (flat.type == 'RENT') {
        houseIcon.image = "/img/cubisthome24.png";
    } else if (flat.type == 'SALE') {
        houseIcon.image = "/img/homesell24.png";
    }

    houseIcon.iconSize = new GSize(24, 24);

    var markerOptions;
    //если свой объект
    if (userid == flat.userid) {
        markerOptions = { icon:houseIcon, draggable:true};
    } else {
        markerOptions = { icon:houseIcon, draggable:false};
    }

    var marker = new GMarker(latlng, markerOptions);
    //финт ушами, иначе не получим flat
    marker.flat = flat;
    flat.marker = marker;

    GEvent.addListener(marker, "click", function() {
        //        var zoom = map.getZoom();
        //        map.setCenter(marker.getLatLng(), zoom);
        centerFlat(marker.flat);
    });

    //если свой объект
    if (userid == flat.userid) {
        GEvent.addListener(marker, "dragend", function() {
            flat.lat = marker.getLatLng().lat();
            flat.lng = marker.getLatLng().lng();
            moveHouse(flat);
        });
    } else {
        GEvent.addListener(marker, "mouseover", function() {
            map.openInfoWindowHtml(marker.getLatLng(), createFlatInfo(flat));
        });
    }

    return marker;
}

function updateMarkerHouse(map, latlng, flat) {
    var houseIcon = new GIcon(G_DEFAULT_ICON);
    if (flat.type == 'RENT') {
        flat.marker.setImage("/img/cubisthome.png");
    } else if (flat.type == 'SALE') {
        flat.marker.setImage("/img/homesell32.png");
    }
    flat.marker.setLatLng(latlng);
}

function centerFlat(flat) {
    var zoom = globalmap.getZoom();
    globalmap.setCenter(new GLatLng(flat.lat, flat.lng, zoom), zoom);
    setResponseToElement('/getflat.do?flatid=' + flat.id, 'properties');
}

function createFlatInfo(flat) {
    var text = '<table onclick="setResponseToElement(\'/getflat.do?flatid=' + flat.id + '\',\'properties\')"><tr>';
    text += '<td>' + '<img src="/img/cubisthome.png"/>' + '</td>';
    text += '<td>' + flat.roomNumber + 'к ' + flat.price + flat.currency + '</td>';
    text += '</tr>';
    text += '<tr><td colspan="2" align="right"><a href="/flat.jsp?id=' + flat.id + '">подробно</a><td></tr>';
    text += '</table>';
    return text;
}

function deleteFlat(flatid) {
    if(confirm('Удалить объект')) {
        globalmap.removeOverlay(houseMap[flatid].marker);
        houseMap[flatid] = undefined;
        setResponseToElement('/deleteflat.do?flatid=' + flatid, 'properties');
    }
}